;(function() {
    init_parsley();
    $.fn.modal.Constructor.prototype.enforceFocus = function () { };

    $('#region_id').on('select2:select', function (e) {
        var data = e.params.data;
        var region_id = data.id;
        if (data.id > 0) {
            $.post(
                "/gym/cabinet/getCabinet",
                {"region_id":region_id},
                function(res){
                    close_loading();
                    if(res.flag){
                        $("#cabinet_id").select2('destroy').empty();
                        $("#cabinet_id").select2({
                            data:res.data
                        });
                    }else{
                        toastr['error'](res.msg,"操作失败！");
                    }
                }
            );
        }
    });
    $('#order_id').select2({
        placeholder: "请输入会员姓名/手机号/卡号",
        minimumInputLength: 2,
        ajax:{
            delay:1000,
            url:"/gym/cabinet/ajaxSearchMember",
            data:function(params){
                var query = {
                    search: params.term,
                    type: 2,
                    order_id: $('#cabinet_order_id').val(),
                    page: params.page || 1
                };
                return query;
            },
            processResults: function (data, params) {
                params.page = params.page || 1;
                return {
                    results: data.results,
                    pagination: {
                        more: data.more
                    }
                };
            },
        }
    });
    $('#order_id').on('select2:select', function (e) {
        var data = e.params.data;
        if (data.id > 0) {
            $('#member_name').val(data.member_name);
            $('#mobile').val(data.mobile);
            $('#gym_card_name').val(data.gym_card_name);
            $('#member_sex').val(data.sex == 1 ? '男' : '女')
        }
    });

    $('#order_date').datepicker({format: 'yyyy-mm-dd',todayHighlight:true,autoclose:true , language:"zh-CN"});

    $("#form-submit").click(function(){
        var flag=$('#validate-form').parsley('validate' );
        if(flag){
            loading();
            $.post(
                "/gym/cabinet/orderTransfer",
                $("#validate-form").serialize(),
                function(data){
                    close_loading();
                    if(data.flag){
                        $("#myModal").modal("hide");
                        toastr['success']("操作成功！", '');
                        $("#myform #btn-advanced-refresh").trigger("click");// 刷新页面
                    }else{
                        toastr['error'](data.msg,"操作失败！");
                    }
                }
            );
        }
    })
})();